<?php
namespace App\Hospital\Controllers;

use App\Admin\Exporter\SignExporter;
use App\Hospital\Repositories\Family;
use App\Imports\HospitalImport;
use App\Jobs\AppointList;
use App\Jobs\HospitalNotice;
use App\Jobs\SyncData;
use App\Models\Appoint;
use App\Models\Area;
use App\Models\FamilyEdu;
use App\Models\Hospital;
use App\Models\HospitalAppoint;
use App\Models\HospitalAppointInterval;
use App\Models\HospitalAppointVaccine;
use App\Models\HospitalImportChild;
use App\Models\HospitalUserFamily;
use App\Models\Interview;
use App\Models\InterviewRemind;
use App\Models\TempPushRecord;
use App\Models\Vaccine;
use App\Wechat\Temp;
use Dcat\Admin\Admin;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;

class ApiController  extends AdminController{


    public function city(Request $request)
    {
        $provinceId = $request->get('q');

        return Area::query()->where('parent_id', $provinceId)->get(['id', DB::raw('title as text')]);
    }

    public function appoint_time(Request $request)
    {
        $provinceId = $request->get('q');
        $hospitalAppoint = HospitalAppoint::query()
            ->where('type',$provinceId)
            ->where('hospital_id',Admin::user()->hospital_id)
            ->first();
        if(!$hospitalAppoint){
            throw new \Exception('未设置所选预约项！');
        }

        return HospitalAppointInterval::query()->where('hospital_appoint_id', $hospitalAppoint->id)->get(['id', DB::raw("CONCAT_WS('-', start, end) as text")]);
    }
    public function project(Request $request)
    {
        $provinceId = $request->get('q');
        if($provinceId==2){
            $haid = HospitalAppoint::query()
                ->where('hospital_id',Admin::user()->hospital_id)
                ->where('type',$provinceId)
                ->value('id');
            if($haid) {
                $vaccines = HospitalAppointVaccine::query()
                    ->where('hospital_appoint_id', $haid)
                    ->pluck('vaccine_id');
            }
            if($vaccines) {
                return Vaccine::query()
                    ->whereIn('id', $vaccines)
                    ->get(['id', DB::raw('name as text')]);
            }else{
                return [];
            }
        }
        return [['id'=>0,'text'=>"儿童体检"]];
    }
    public function familys(Request $request)
    {
        $q = $request->get('q');
        $sql = "SET sql_mode = (SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));";
        DB::statement($sql);

        $familys =\App\Models\Family::query()
            ->join('eb_hospital_user_family','eb_family.id','=','eb_hospital_user_family.family_id')
            ->where('eb_hospital_user_family.hospital_id',Admin::user()->hospital_id)
            ->where('eb_family.name', 'like', "%$q%")
            ->where('eb_family.type',1)
            ->groupBy('eb_hospital_user_family.family_id')
            ->paginate(null, ['eb_family.id', DB::raw("CONCAT(eb_family.name, '/', eb_family.birthday,'/',eb_family.phone) as text")]);

        return $familys;
    }
}
